package com.spring.security.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @ClassName MyUserDetailService
 * @Description MyUserDetailService
 * @Author 朱秒
 * @Date 2020/2/22 12:07
 * @Version 1.0
 */
@Component
public class MyUserDetailService  implements UserDetailsService {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("登录用户名:{}", username);
        //这里应该从数据库拿出用户信息，暂时剥离写死
        //这里可以自己判断用户是否-锁定-删除-过期等，然后返回给User对象
        //模拟数据库里面的加密密码
        String password = passwordEncoder.encode("123456");
        logger.info("数据库密码:{}", password);
        return new User(username, password,
                true,true,true,true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
